package com.common.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.api.exception.CommonException;
import com.common.util.SqlParamsUtil;

@Service
public class SqlDaoService {

    @Autowired
    private SqlDao commonSqlDao;
 
	public Integer selectInt(String sql) {
		validate(sql);
		return commonSqlDao.selectInt(sql);
	}

	public String selectString(String sql) {
		validate(sql);
		return commonSqlDao.selectString(sql);
	}
	
	public Map<String,Object> selectObject(String sql){
		validate(sql);
		return commonSqlDao.selectObject(sql);
	}
	
	public List<Map<String, Object>> selectObjectList(String sql){
		validate(sql);
		return commonSqlDao.selectObjectList(sql);
	}
	
	public void execute(String sql) {
		validate(sql);
		commonSqlDao.execute(sql);
	}

	public <T> T selectTypeObject(String sql) {
		validate(sql);
		return commonSqlDao.selectTypeObject(sql);
	}

	public <T> List<T> selectTypeList(String sql) {
		validate(sql);
		return commonSqlDao.selectTypeList(sql);
	}
	
	private void validate(String sql) {
		if(SqlParamsUtil.validSqlInjection(sql)) {
			throw new CommonException("存在sql注入风险!");
		}
	}
 }
